package com.hotel.homehotel.before.controller;

import com.hotel.homehotel.before.entiy.Order;
import com.hotel.homehotel.before.entiy.Room;
import com.hotel.homehotel.before.entiy.User;
import com.hotel.homehotel.before.entiy.vo.OrderVo;
import com.hotel.homehotel.before.service.OrderService;
import com.hotel.homehotel.before.service.RoomService;
import com.hotel.homehotel.before.service.UserService;
import com.hotel.homehotel.utils.ConversionUtils;
import com.hotel.homehotel.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@RestController
@Api(tags="订单管理" ,value="订单管理")
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    @Autowired
    OrderService orderService;
@Autowired
    UserService userService;
@Autowired
    RoomService roomService;
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "获取所有订单信息")
    @PostMapping("/getAllOrders")
    public R getAllOrders(){
        List<OrderVo> list=new LinkedList<>();//插入效率比较高
        List<Order> allOrders = orderService.getAllOrders();
        for(Order order :allOrders){
            User userById = userService.getUserById(order.getUserId());
            Room roomById = roomService.getRoomById(order.getRoomId());
             OrderVo orderVo=new OrderVo(order, ConversionUtils.toUserVo(userById),roomById);
             list.add(orderVo);

        }
        return R.define(true,200,"查询成功").data("orders",list);
    }

    @ApiOperation(value = "获取某个用户下的订单信息 你上传git 我来处理吧 好 那个token是叫什么 这个还是啥 ")
    @GetMapping("/getAllOrdersByUid/{uid}")
    public R getAllOrdersByPhoneNumber(@ApiParam(name="uid",value = "用户id",required = true)
                                           @PathVariable String  uid ){
        List<OrderVo> list=new LinkedList<>();//插入效率比较高
        List<Order> allOrders = orderService.getAllOrdersByUid(uid);
        for(Order order :allOrders){
            User userById = userService.getUserById(order.getUserId());
            Room roomById = roomService.getRoomById(order.getRoomId());
            OrderVo orderVo=new OrderVo(order, ConversionUtils.toUserVo(userById),roomById);
            list.add(orderVo);

        }
        return R.define(true,200,"查询成功").data("orders",list);
    }



    @ApiOperation(value = "添加订单")
    @PostMapping("/insertOrder")
    public R insertOrder(@RequestBody  Order order){
        boolean b = orderService.insertOrder(order);
        if(b)
            return  R.define(true,200,"添加成功");
        return  R.define(false,400,"添加失败");


    }
    @ApiOperation(value = "删除订单")
    @GetMapping("/deleteOrder/{id}")
    public  R  deleteOrder(@ApiParam(name="id",value = "订单id",required = true)
                               @PathVariable String id){
        boolean b = orderService.deleteOrder(id);
        if(b)
            return  R.define(true,200,"删除成功");
        return  R.define(false,400,"删除失败");
    }

    @ApiOperation(value = "修改订单")
    @PostMapping("/updateOrder")
    public  R  updateOrder(@RequestBody  Order order){
        boolean b = orderService.updateOrder(order);
        if(b)
            return  R.define(true,200,"修改成功");
        return  R.define(false,400,"修改失败");
    }
    @ApiOperation(value = "获取当前订单详情")
    @GetMapping("/getOrderById/{id}")
    public   R  getOrderById( @ApiParam(name="id",value = "订单id",required = true)
    @PathVariable String id){
        Order order = orderService.getOrderById(id);
        User userById = userService.getUserById(order.getUserId());
        Room roomById = roomService.getRoomById(order.getRoomId());
        OrderVo orderVo=new OrderVo(order, ConversionUtils.toUserVo(userById),roomById);
            return  R.define(true,200,"查询成功").data("order",orderVo);
    }
}
